<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *      [PinTuXiu] (C)2001-2099 ONightjar.com Pintuxiu.com.
 *      This is NOT a freeware, use is subject to license terms
*/

class category_api {

	public function getRootCategories(){
		return load_db('category')->getRootCategroies();
	}

	public function getNameCategories(){
		return load_db('category')->getNameCategories();
	}

	public function getAllCategories(){
		return load_db('category')->getAllCategories();
	}

	public function getCategories($page,$pagesize){
		return load_db('category')->search($page,$pagesize);
	}

	public function getLastLevelCategories(){
		return load_db('category')->getLastLevelCategories();
	}

	public function getCategoryByid($id,$cache=false){
		$categorydb = load_db('category');
		if($cache){
			$categories = $categorydb->getAllCategories();
			return $categories?$categories[$id]:false;
		}else{
			return $categorydb->findBy('category_id',$id);
		}
	}

	public function getCategoryTree(){
		$categorydb = load_db('category');
		return $categorydb->getCategoryTree();
	}

	public function getCleanCategoryTree(){
		$categorydb = load_db('category');
		return $categorydb->getCleanCategoryTree();
	}

	public function getCategoryDeepTree(){
		$categorydb = load_db('category');
		return $categorydb->getCategoryDeepTree();
	}

	public function getCategoryDeepTreeTpl(){
		$categorydb = load_db('category');
		return $categorydb->getCategoryDeepTreeTpl();
	}

	public function getCategoryByENName($enname){
		if($enname){
			$enname = strtolower($enname);
			$categories = $this->getNameCategories();
			return $categories[$enname];
		}
		return false;
	}

	public function deleteCategoryByid($id){
		$categorydb = load_db('category');
		$result = $categorydb->delete(array('category_id'=>$id));
		if($result){
			$categorydb->updateCache();
			$systemcate = $categorydb->findBy('is_system','1');
			if($systemcate){
				load_db('share')->update(array('category_id'=>$id),array('category_id'=>$systemcate['category_id']));
				load_db('tag')->update(array('category_id'=>$id),array('category_id'=>$systemcate['category_id']));
				load_db('album')->update(array('category_id'=>$id),array('category_id'=>$systemcate['category_id']));
				load_db('topic')->update(array('category_id'=>$id),array('category_id'=>$systemcate['category_id']));
				load_db('group')->update(array('category_id'=>$id),array('category_id'=>$systemcate['category_id']));
			}
		}
		return $result;
	}

	public function addCategory($data){
		$categorydb = load_db('category');
		$result = $categorydb->create($data);
		if($result) $categorydb->updateCache();
		return $result;
	}

	public function updateCategoryByid($id,$update){
		$categorydb = load_db('category');
		$result = $categorydb->update(array('category_id'=>$id),$update);
		if($result) $categorydb->updateCache();
		return $result;
	}

	public function getSubCategories($cid){
		/* $categpryTree = $this->getCategoryTree();
		 if($categpryTree['cat'.$cid])
			return $categpryTree['cat'.$cid]['subcats'];
		return false; */
		return load_db('category')->getSubCategories($cid);
	}

	public function getRootCategroiesFromDb(){
		return load_db('category')->getRootCategroiesFromDb();
	}

	public function getCategoryHotTags($cid){
		$category = $this->getCategoryByid($cid,true);
		$hotwords = $category['category_hot_words'];
		$tags = explode(',', $hotwords);
		$clean = array();
		foreach ($tags as $tag){
			$clean[] = trim($tag);
		}
		return $clean;
	}

	public function getClearSearchKeywords($tags){
		$clean = array();
		foreach ($tags as $tag){
			$tag = trim($tag);
			$clean[] = array('name'=>$tag,'search'=>pzxencode($tag));
		}

		return $clean;
	}

	public function getCategoryWithHomeShares(){
		$categories = load_db('category')->get_category_top();
		$pz = &get_instance();
		foreach ($categories as $key=>$category){
			if($category['is_home']){
				$hotwords = $category['category_hot_words'];
				$tags = explode(',', $hotwords);
				$categories[$key]['tags'] = $this->getClearSearchKeywords($tags);
				//$style = (in_array($category['style'], array('home_1','home_2','home_3')))?$category['style']:'home_1';
				$style = (!is_empty($category['style']))?$category['style']:'home_1';
				$categories[$key]['home_view'] = $pz->render('common/'.$style,array('category'=>$categories[$key]));
			}
		}

		return $categories;
	}

	function add_totalshare($cid){
		return load_db('category')->add_totalshare($cid);
	}

	function remove_totalshare($cid){
		return load_db('category')->remove_totalshare($cid);
	}

	function get_subcat_ids($cid,&$ids){
		$categories = $this->getSubCategories($cid);
		if($categories&&array_length($categories)>0){
			foreach ($categories as $category){
				array_push($ids, $category['category_id']);
				$this->get_subcat_ids($category['category_id'], $ids);
			}
		}
	}


	function check_totalshare(){
		$shareapi = load_api('share');
		$categorydb = load_db('category');
		$categories = $categorydb->getAllCategories();
		foreach ($categories as $category){
			$ids = array($category['category_id']);
			$this->get_subcat_ids($category['category_id'], $ids);
			$count = $shareapi->countByCategories($ids);
			$categorydb->update(array('category_id'=>$category['category_id']),array('total_share'=>$count));
		}

		/* $roots = $categorydb->getAllCategories();

		foreach ($roots as $category){
		$categorydb->check_subtotalshare($category['category_id']);
		} */

		return $categorydb->updateCache();
	}

}

